﻿using Competition.Admin.Web.Models.NewsViewModels;
using Competition.Admin.Web.ResultModels;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;
using Competition.Admin.Web.Models.IntroductionViewModels;
using Competition.Admin.Web.ResultModels.IntroductionResultModels;
using Competition.Domain.ValueObjects;

namespace Competition.Admin.Web.Controllers
{
    public class IntroductionController : Controller
    {
        private readonly Lazy<IIntroductionService> _introductionService;  //资讯业务
        public IntroductionController(Lazy<IIntroductionService> introductionService)
        {
            _introductionService = introductionService;
        }
        // GET: News
        public ActionResult Index()
        {
            return View();
        }

        // GET: News/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: News/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: News/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([FromForm]IntroductionViewModel model)
        {
            if (ModelState.IsValid)
            {
                MessageBase result = new MessageBase();

                var dto = new IntroductionDto
                {
                    Title = model.Title,
                    Description = model.Description,
                    Html = model.Html,
                    Text = model.Text,
                    ClassId = (IntroductionEnum)model.ClassId,
                    Status = model.Status
                };
                int id = await _introductionService.Value.AddAsync(dto);
                result.Success = id > 0;
                return Json(result);
            }

            return View(nameof(Create), model);
        }

        // GET: News/Edit/5
        public async Task<ActionResult> Edit(int id)
        {
            var dto = await  _introductionService.Value.FindAsync(id);
            var model = new IntroductionViewModel()
            {
                Id = dto.Id,
                Title = dto.Title,
                Html = dto.Html,
                Text = dto.Text,
                Status = dto.Status,
                Description = dto.Description,
                ClassId = (int)dto.ClassId
            };
            return View(nameof(Edit), model);
        }

        // POST: News/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit(int id, [FromForm]IntroductionViewModel model)
        {
            if (ModelState.IsValid)
            {
                MessageBase result = new MessageBase();
                var dto = new IntroductionDto
                {
                    Id = id,
                    Title = model.Title,
                    Description = model.Description,
                    Html = model.Html,
                    Text = model.Text,
                    ClassId = (IntroductionEnum)model.ClassId,
                    Status = model.Status
                };

                result.Success = await _introductionService.Value.UpdateAsync(dto);
                return Json(result);
            }

            return View(nameof(Edit), model);
        }

        // POST: News/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Delete(int id)
        {
            MessageBase result = new MessageBase();
            result.Success = await _introductionService.Value.DeleteAsync(id);
            return Json(result);
        }

        /// <summary>
        /// 根据条件查找新闻列表
        /// </summary>
        /// <param name="page">页面索引</param>
        /// <param name="limit">页面大小</param>
        /// <returns>新闻列表</returns>
        [HttpGet, ActionName(nameof(DataJson))]
        public async Task<IActionResult> DataJson(int page, int limit)
        {
            var v = await _introductionService.Value.FindListAsync(page, limit);
            var model = v.dtos.Select(dto => new IntroductionResultModel()
            {
                Id = dto.Id,
                AddTime = dto.AddTime.ToString("yyyy-MM-dd HH:mm:ss"),
                Title = dto.Title,
                Status = dto.Status,
                ClassId = dto.ClassId.ToString()
            }).ToList();

            var result = new
            {
                Code = 0,
                Msg = "",
                Count = v.pageDto.TotalRecord,
                Data = model
            };
            return new JsonResult(result);
        }
    }
}